package org.example.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RedisClient {
    public static void main(String[] args) {
        final NioEventLoopGroup group = new NioEventLoopGroup();
        ChannelFuture channelFuture = new Bootstrap()
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {

                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        ChannelPipeline pipeline = nioSocketChannel.pipeline();
                        pipeline.addLast(new LoggingHandler(LogLevel.INFO));
                        pipeline.addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                super.channelActive(ctx);
                                log.info("链接建立了");
                                for (int i = 0; i < 10; i++) {

                                    extracted(ctx,i);
                                }
                            }

                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("redis服务器发送来的消息为{}", msg);
                                super.channelRead(ctx, msg);
                                ctx.channel().close();
                            }
                        });
                    }

                    private void extracted(final ChannelHandlerContext ctx,int i) {
                        final byte[] LINE = new byte[]{13, 10};
                        ByteBuf buffer = ctx.alloc().buffer();
                        buffer.writeBytes("*3".getBytes());
                        buffer.writeBytes(LINE);
                        buffer.writeBytes("$3".getBytes());
                        buffer.writeBytes(LINE);
                        buffer.writeBytes("set".getBytes());
                        buffer.writeBytes(LINE);
                        buffer.writeBytes("$3".getBytes());
                        buffer.writeBytes(LINE);
                        String key = new StringBuilder().append("wb").append(i).toString();
                        buffer.writeBytes(key.getBytes());
                        buffer.writeBytes(LINE);
                        buffer.writeBytes("$5".getBytes());
                        buffer.writeBytes(LINE);
                        String value = new StringBuilder().append("jobs").append(i).toString();
                        buffer.writeBytes(value.getBytes());
                        buffer.writeBytes(LINE);
                        ctx.writeAndFlush(buffer);
                    }
                })
                .connect("127.0.0.1", 6379);
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if (channelFuture.isSuccess()) {
                    log.info("链接成功");
                } else {
                    log.info("链接失败");
                }

            }
        });
        ChannelFuture closeFuture = channelFuture.channel().closeFuture();
        closeFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                log.info("关闭成功");
                group.shutdownGracefully();

            }
        });
    }
}
